package com.darwin.darwinUI.controls
{
	import com.darwin.darwinUI.core.UIComponent;
	import com.darwin.darwinUI.core.UISprite;
	
	import flash.display.DisplayObject;
	import flash.geom.Rectangle;
	/**
	 * 进度条组件 进度条组件包含两个皮肤 background:进度条背景 bar:进度条内容</br>
	 * background大小 应跟 bar大小是对应的
	 * 
	 * @author vanCopper
	 * 
	 */	
	public class ProgressBar extends UIComponent
	{
		private var _bar:DisplayObject;
		private var _barSkin:String;
		private var _barContainer:UISprite;
		private var _progress:Number = 0;
		private var _barOffX:Number = 0;
		private var _barOffY:Number = 0;
		
		private var _scaleModel:Boolean = false;
		
		public function ProgressBar()
		{
			super();
		}
		
		override protected function initUIComponent():void
		{
			super.initUIComponent();
			this.backgroundAuto = true;
		}
		
		/**
		 * 更新进度 
		 * 
		 */		
		protected function updateProgress():void
		{
			if(bar)
			{
				if(progress <= 1)
				{
					if(!scaleModel)
					{
						bar.scrollRect = new Rectangle(0,0,_barW*progress,_barH);
					}else
					{
						bar.scaleX = progress;
					}
				}
			}
		}

		/**
		 * 进度条内容皮肤 
		 */
		public function get bar():DisplayObject
		{
			return _bar;
		}

		private var _barChange:Boolean = false;
		/**
		 * @private
		 */
		public function set bar(value:DisplayObject):void
		{
			_bar = value;
			_barChange = true;
			if(this.stage)this.stage.invalidate();
		}

		/**
		 * 进度条内容 
		 */
		public function get barSkin():String
		{
			return _barSkin;
		}

		/**
		 * @private
		 */
		public function set barSkin(value:String):void
		{
			_barSkin = value;
			if(_barSkin)
			{
				binding(_barSkin,"bar");
			}
		}

		/**
		 * 进度值 0-1  
		 */
		public function get progress():Number
		{
			return _progress;
		}

		/**
		 * @private
		 */
		public function set progress(value:Number):void
		{
			_progress = value;
			updateProgress();
		}
		
		private var _barW:Number = 0;
		private var _barH:Number = 0;
		override protected function refresh():void
		{
			super.refresh();
			
			if(_barChange)
			{
				if(bar && bar.parent)
				{
					bar.parent.removeChild(bar);
				}
				_barW = bar.width;
				_barH = bar.height;
				addChild(bar);
				bar.x = barOffX;
				bar.y = barOffY;
				_barChange = false;
			}
		}

		/**
		 * bar的X偏移量 
		 */
		public function get barOffX():Number
		{
			return _barOffX;
		}

		/**
		 * @private
		 */
		public function set barOffX(value:Number):void
		{
			_barOffX = value;
			_barChange = true;
		}

		/**
		 * bar的Y偏移量 
		 */
		public function get barOffY():Number
		{
			return _barOffY;
		}

		/**
		 * @private
		 */
		public function set barOffY(value:Number):void
		{
			_barOffY = value;
			_barChange = true;
		}

		/**
		 * 进度条更新方式 true:进度条将通过改变scaleX来更新进度 
		 */
		public function get scaleModel():Boolean
		{
			return _scaleModel;
		}

		/**
		 * @private
		 */
		public function set scaleModel(value:Boolean):void
		{
			_scaleModel = value;
		}


	}
}